# Copyright 2010, Oracle and/or its affiliates. All rights reserved.
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1335  USA

SET(CAN_BUILD_MSI 1)
MACRO(CANT_BUILD_MSI reason)
  IF(BUILD_RELEASE)
    MESSAGE(FATAL_ERROR "Can't build MSI package - ${reason}")
  ENDIF()
  SET(CAN_BUILD_MSI 0)
ENDMACRO()

IF (NOT CMAKE_C_COMPILER_ARCHITECTURE_ID)
  CANT_BUILD_MSI("Can't determine compiler architecture")
ENDIF()


STRING(TOLOWER "${CMAKE_C_COMPILER_ARCHITECTURE_ID}" WIX_ARCH)

SET(WIX_BIN_PATHS)
FOREACH(WIX_VER 3.14 3.13 3.12 3.11)
  LIST(APPEND WIX_BIN_PATHS "$ENV{ProgramFiles}/WiX Toolset v${WIX_VER}/bin")
  LIST(APPEND WIX_BIN_PATHS "$ENV{ProgramFiles} (x86)/WiX Toolset v${WIX_VER}/bin")
ENDFOREACH()
FIND_PATH(WIX_DIR heat.exe ${WIX_BIN_PATHS})
IF(NOT WIX_DIR)
  CANT_BUILD_MSI("WiX version 3.11 or later not found")
ENDIF()


GET_FILENAME_COMPONENT(WIX_SDK_DIR  ../SDK/ ABSOLUTE BASE_DIR ${WIX_DIR} CACHE)
FIND_LIBRARY(WIX_WCAUTIL_LIBRARY
  wcautil
  PATHS
  ${WIX_SDK_DIR}/VS2017/lib/${WIX_ARCH}
  ${WIX_SDK_DIR}/VS2015/lib/${WIX_ARCH}
  )

IF(NOT WIX_WCAUTIL_LIBRARY)
  CANT_BUILD_MSI("wcautil.lib not found for ${WIX_ARCH}")
ENDIF()

FIND_LIBRARY(WIX_DUTIL_LIBRARY 
  dutil
  PATHS 
  ${WIX_SDK_DIR}/VS2017/lib/${WIX_ARCH}
  ${WIX_SDK_DIR}/VS2015/lib/${WIX_ARCH}
  )

IF(NOT WIX_DUTIL_LIBRARY)
  CANT_BUILD_MSI("dutil.lib not found for ${WIX_ARCH}")
ENDIF()

FIND_PATH(WIX_INCLUDE_DIR
  wcautil.h PATHS
  ${WIX_SDK_DIR}/VS2017/inc
  ${WIX_SDK_DIR}/VS2015/inc
  ${WIX_SDK_DIR}/inc)

IF(NOT WIX_INCLUDE_DIR)
  CANT_BUILD_MSI("wcautil.h  not found for ${WIX_ARCH}")
ENDIF()

FIND_PROGRAM(CANDLE_EXECUTABLE candle ${WIX_DIR})
IF(NOT CANDLE_EXECUTABLE)
  CANT_BUILD_MSI("candle.exe not found")
ENDIF()

FIND_PROGRAM(LIGHT_EXECUTABLE light ${WIX_DIR})
IF(NOT LIGHT_EXECUTABLE)
  CANT_BUILD_MSI("light.exe not found")
ENDIF()

SET(CPACK_WIX_PACKAGE_BASE_NAME "MariaDB")

SET(CPACK_WIX_UPGRADE_CODE_arm64 "5AA9B79C-643C-4151-811D-B6845AA5DB28")
SET(CPACK_WIX_UPGRADE_CODE_x86 "49EB7A6A-1CEF-4A1E-9E89-B9A4993963E3")
SET(CPACK_WIX_UPGRADE_CODE_x64 "2331E7BD-EE58-431B-9E18-B2B918BCEB1B")

SET(CPACK_WIX_UPGRADE_CODE ${CPACK_WIX_UPGRADE_CODE_${WIX_ARCH}})
IF(NOT CPACK_WIX_UPGRADE_CODE)
  MESSAGE_ONCE("unknown upgrade code for arch ${WIX_ARCH}")
  CANT_BUILD_MSI("unknown upgrade code for arch ${WIX_ARCH}")
ENDIF()

IF(WIX_ARCH STREQUAL "x86")
 SET(CPACK_WIX_PACKAGE_NAME "MariaDB ${MAJOR_VERSION}.${MINOR_VERSION}")
ELSE()
 SET(CPACK_WIX_PACKAGE_NAME "MariaDB ${MAJOR_VERSION}.${MINOR_VERSION} (${WIX_ARCH})")
ENDIF()

IF(CAN_BUILD_MSI)

ADD_SUBDIRECTORY(ca)
SET(MANUFACTURER "MariaDB Corporation Ab")



# WiX wants the license text as rtf; if there is no rtf license, 
# we create a fake one from the plain text COPYING file.
IF(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/COPYING.rtf")
 SET(COPYING_RTF "${CMAKE_CURRENT_SOURCE_DIR}/COPYING.rtf")
ELSE()
  IF(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../../LICENSE.mysql")
    SET(LICENSE_FILE "${CMAKE_CURRENT_SOURCE_DIR}/../../LICENSE.mysql")
  ELSE()
    SET(LICENSE_FILE "${CMAKE_CURRENT_SOURCE_DIR}/../../COPYING")
  ENDIF()
  FILE(READ ${LICENSE_FILE} CONTENTS)
  STRING(REGEX REPLACE "\n" "\\\\par\n" CONTENTS "${CONTENTS}")
  STRING(REGEX REPLACE "\t" "\\\\tab" CONTENTS "${CONTENTS}")
  FILE(WRITE "${CMAKE_CURRENT_BINARY_DIR}/COPYING.rtf" "{\\rtf1\\ansi\\deff0{\\fonttbl{\\f0\\fnil\\fcharset0 Courier New;}}\\viewkind4\\uc1\\pard\\lang1031\\f0\\fs15")
  FILE(APPEND "${CMAKE_CURRENT_BINARY_DIR}/COPYING.rtf" "${CONTENTS}")
  FILE(APPEND "${CMAKE_CURRENT_BINARY_DIR}/COPYING.rtf" "\n}\n")
  SET(COPYING_RTF "${CMAKE_CURRENT_BINARY_DIR}/COPYING.rtf")
ENDIF()
SET(CPACK_WIX_CONFIG ${CMAKE_CURRENT_SOURCE_DIR}/CPackWixConfig.cmake)

IF(NOT TARGET mariadb-upgrade-wizard)
  SET(EXTRA_WIX_PREPROCESSOR_FLAGS "-dHaveUpgradeWizard=0")
ENDIF()
IF(WITH_INNOBASE_STORAGE_ENGINE)
  SET(EXTRA_WIX_PREPROCESSOR_FLAGS ${EXTRA_WIX_PREPROCESSOR_FLAGS} "-dHaveInnodb=1")
ENDIF()

SET(THIRD_PARTY_FEATURE_CONDITION "")

IF(WITH_THIRD_PARTY)
  SET(THIRD_PARTY_DOWNLOAD_LOCATION "$ENV{TEMP}")
  IF(THIRD_PARTY_DOWNLOAD_LOCATION)
    FILE(TO_CMAKE_PATH "${THIRD_PARTY_DOWNLOAD_LOCATION}" THIRD_PARTY_DOWNLOAD_LOCATION)
  ELSE()
    SET(THIRD_PARTY_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}")
  ENDIF()
ENDIF()

FOREACH(third_party ${WITH_THIRD_PARTY})
  SET(third_party_install_plugin ${CMAKE_CURRENT_SOURCE_DIR}/${third_party}.cmake)
  IF(NOT EXISTS ${third_party_install_plugin})
    MESSAGE(FATAL_ERROR 
"Third party MSI installation plugin ${third_party_install_plugin} does not exist.
It was expected due to WITH_THIRD_PARTY=${WITH_THIRD_PARTY}"
)
  ENDIF()
  STRING(TOUPPER "${third_party}" upper_third_party)
  IF(NOT THIRD_PARTY_FEATURE_CONDITION )
   SET(THIRD_PARTY_FEATURE_CONDITION "<Condition Level='0'>${upper_third_party}INSTALLED")
  ELSE()
   SET(THIRD_PARTY_FEATURE_CONDITION "AND ${upper_third_party}INSTALLED")
  ENDIF()
ENDFOREACH()

IF(THIRD_PARTY_FEATURE_CONDITION)
  SET(THIRD_PARTY_FEATURE_CONDITION "${THIRD_PARTY_FEATURE_CONDITION}</Condition>")
ENDIF()

IF(CMAKE_GENERATOR MATCHES "Visual Studio")
 SET(CONFIG_PARAM "-DCMAKE_INSTALL_CONFIG_NAME=${CMAKE_CFG_INTDIR}")
ENDIF()


ADD_CUSTOM_TARGET(
  MSI
  COMMAND ${CMAKE_COMMAND} ${CONFIG_PARAM}
    -DCANDLE_EXECUTABLE="${CANDLE_EXECUTABLE}"
    -DCMAKE_CFG_INTDIR="${CMAKE_CFG_INTDIR}"
    -DCMAKE_FULL_VER="${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}.${CMAKE_PATCH_VERSION}"
    -DCMAKE_SIZEOF_VOID_P=${CMAKE_SIZEOF_VOID_P}
    -DCOPYING_RTF="${COPYING_RTF}"
    -DCPACK_WIX_CONFIG="${CPACK_WIX_CONFIG}"
    -DCPACK_WIX_INCLUDE="${CPACK_WIX_INCLUDE}"
    -DCPACK_WIX_PACKAGE_BASE_NAME="${CPACK_WIX_PACKAGE_BASE_NAME}"
    -DCPACK_WIX_PACKAGE_NAME="${CPACK_WIX_PACKAGE_NAME}"
    -DCPACK_WIX_UPGRADE_CODE="${CPACK_WIX_UPGRADE_CODE}"
    -DEXTRA_WIX_PREPROCESSOR_FLAGS="${EXTRA_WIX_PREPROCESSOR_FLAGS}"
    -DLIGHT_EXECUTABLE="${LIGHT_EXECUTABLE}"
    -DMAJOR_VERSION="${MAJOR_VERSION}"
    -DMANUFACTURER="${MANUFACTURER}"
    -DMINOR_VERSION="${MINOR_VERSION}"
    -DPATCH_VERSION="${PATCH_VERSION}"
    -DSIGNCODE="${SIGNCODE}"
    -DSIGNTOOL_EXECUTABLE="${SIGNTOOL_EXECUTABLE}"
    -DSIGNTOOL_PARAMETERS="${SIGNTOOL_PARAMETERS}"
    -DSRCDIR="${CMAKE_CURRENT_SOURCE_DIR}"
    -DTHIRD_PARTY_DOWNLOAD_LOCATION="${THIRD_PARTY_DOWNLOAD_LOCATION}"
    -DTHIRD_PARTY_FEATURE_CONDITION="${THIRD_PARTY_FEATURE_CONDITION}"
    -DTINY_VERSION="${TINY_VERSION}"
    -DTOP_BINDIR="${CMAKE_BINARY_DIR}"
    -DVERSION="${VERSION}"
    -DWITH_THIRD_PARTY="${WITH_THIRD_PARTY}"
    -DWIXCA_LOCATION="$<TARGET_FILE:wixca>"
    -DMSVC_CRT_TYPE="${MSVC_CRT_TYPE}"
    -DDYNAMIC_UCRT_LINK="${DYNAMIC_UCRT_LINK}"
    -DPlatform="${WIX_ARCH}"
    -P  ${CMAKE_CURRENT_SOURCE_DIR}/create_msi.cmake
)
ADD_DEPENDENCIES(MSI wixca)

ENDIF(CAN_BUILD_MSI)

IF(CMAKE_GENERATOR MATCHES "Visual Studio")
 SET(CPACK_CONFIG_PARAM -C $(Configuration))
ENDIF()

IF(SIGNCODE)
  SET(SIGN_COMMAND COMMAND ${CMAKE_COMMAND} -P ${PROJECT_BINARY_DIR}/sign.cmake)
ENDIF()

ADD_CUSTOM_TARGET(
  win_package_zip
  ${SIGN_COMMAND}
  COMMAND ${CMAKE_CPACK_COMMAND} ${CPACK_CONFIG_PARAM} --config ${CMAKE_CURRENT_SOURCE_DIR}/CPackZipConfig.cmake
  WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)

ADD_CUSTOM_TARGET(
  win_package_debuginfo
  COMMAND ${CMAKE_CPACK_COMMAND} ${CPACK_CONFIG_PARAM} --config ${CMAKE_CURRENT_SOURCE_DIR}/CPackZipDebugInfoConfig.cmake
  WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)

ADD_CUSTOM_TARGET(
  win_package_test
  COMMAND ${CMAKE_CPACK_COMMAND} ${CPACK_CONFIG_PARAM} --config ${CMAKE_CURRENT_SOURCE_DIR}/CPackZipTestConfig.cmake
  WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)

ADD_CUSTOM_TARGET(win_package DEPENDS win_package_zip win_package_debuginfo)
SET_TARGET_PROPERTIES(
  win_package win_package_zip win_package_debuginfo
  PROPERTIES
  EXCLUDE_FROM_ALL TRUE
  EXCLUDE_FROM_DEFAULT_BUILD TRUE
)
